package com.zhny.aiot.gdsfj.plugin.model;

import com.zhny.aiot.plugin.core.plugin.AbstractDevice;
import com.zhny.aiot.plugin.modbus.device.ModemDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.MessageEntry;
import com.zhny.aiot.plugin.model.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FertilizerCoilResultModel extends FertilizerResultModel {
    private final Logger logger = LoggerFactory.getLogger(FertilizerCoilResultModel.class);
    public FertilizerCoilResultModel(ModemResponseMessage modemBusResponse, ModemDevice device) {
        super (modemBusResponse, device);
    }

    @Override
    protected void onDecoder(MessageEntry entry) {
        logger.info("---fertilizer writer coil message---");
        int address = getModemBusResponse().getSize();
        int dataValue = getModemBusResponse().getData().readUnsignedShort();
        FertilizerStoreEntry storeEntry = new FertilizerStoreEntry();
        if (address == 24){
            if(dataValue == 65280){
                storeEntry.setPumpStatus(true);
            }
            else {
                storeEntry.setPumpStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 0x40){
            if(dataValue == 65280){
                storeEntry.setMainFlowStatus(true);
            }
            else {
                storeEntry.setMainFlowStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 25){
            if(dataValue == 65280){
                storeEntry.setChannelAStatus(true);
            }
            else {
                storeEntry.setChannelAStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 26){
            if(dataValue == 65280){
                storeEntry.setChannelBStatus(true);
            }
            else {
                storeEntry.setChannelBStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 27){
            if(dataValue == 65280){
                storeEntry.setChannelCStatus(true);
            }
            else {
                storeEntry.setChannelCStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 58){
            if(dataValue == 65280){
                storeEntry.setChannelDStatus(true);
            }
            else {
                storeEntry.setChannelDStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 0x3c){
            if(dataValue == 65280){
                storeEntry.setChannelEStatus(true);
            }
            else {
                storeEntry.setChannelEStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 0x3e){
            if(dataValue == 65280){
                storeEntry.setChannelFStatus(true);
            }
            else {
                storeEntry.setChannelFStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 54){
            if(dataValue == 65280){
                storeEntry.setBucketAStatus(true);
            }
            else {
                storeEntry.setBucketAStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 55){
            if(dataValue == 65280){
                storeEntry.setBucketBStatus(true);
            }
            else {
                storeEntry.setBucketBStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 56){
            if(dataValue == 65280){
                storeEntry.setBucketCStatus(true);
            }
            else {
                storeEntry.setBucketCStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 57){
            if(dataValue == 65280){
                storeEntry.setBucketDStatus(true);
            }
            else {
                storeEntry.setBucketDStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 0x5d){
            if(dataValue == 65280){
                storeEntry.setBucketDStatus(true);
            }
            else {
                storeEntry.setBucketDStatus(false);
            }
            entry.setData(storeEntry);
        }
        else if (address == 0x5e){
            if(dataValue == 65280){
                storeEntry.setBucketDStatus(true);
            }
            else {
                storeEntry.setBucketDStatus(false);
            }
            entry.setData(storeEntry);
        }
        else{
            entry.setType (MessageType.ERROR);
            entry.setData("no found response decoder!");
        }

        getDevice().nofity(entry);
        getDevice().saveReal(storeEntry);
    }
}
